const app = getApp()
let qCloud = require("../../index.js");

Page({
  /** 页面的初始数据 */
  data: {
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
    workReportApprovalList: [], // 生产任务列表
    taskInfo: {}, // 上数信息
    approvalResult: 0, // 审批结果:1-审批通过;2-审批拒绝;
    throughQuantity: null, // 通过数量:1-审批通过时必送
    rejectReason: null, // 拒绝原因:2-审批拒绝时必送
    unfinishedQuantity: 0, // 审核显示未完成数量
  },
  // 通过数量
  throughQuantityInput(e) {
    this.setData({
      throughQuantity: e.detail.value
    });
  },
  // 拒绝原因
  rejectReasonInput(e) {
    this.setData({
      rejectReason: e.detail.value
    });
  },
  // 显示弹窗
  showmodalTap(e) {
    let self = this;
    self.setData({
      taskInfo: e.currentTarget.dataset.info,
      approvalResult: e.currentTarget.dataset.ar,
      unfinishedQuantity: e.currentTarget.dataset.uq,
    });
  },
  // 关闭弹窗
  closemodalTap(e) {
    this.setData({
      taskInfo: {},
      approvalResult: 0,
      throughQuantity: null,
      rejectReason: null,
    });
  },
  /** 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    // 查询上数审批列表
    this.queryUpcountApprovalList();
  },
  // 查询上数审批列表
  queryUpcountApprovalList() {
    let self = this;
    let params = {};
    app.fetchApis(self, qCloud.api.workreport_queryWorkReportApprovalList, params, "POST", function (e) {
      let result = e.data;
      if (result.code == 200) {
        let data = result.data;
        self.setData({
          workReportApprovalList: data.workReportApprovalList,
          approvalResult: 0, // 审批结果:1-审批通过;2-审批拒绝;
          throughQuantity: null, // 通过数量:1-审批通过时必送
          rejectReason: null, // 拒绝原因:2-审批拒绝时必送
        });
      }
    });
  },
  // 提交上数审批结果
  submitUpcountApproval() {
    let self = this;

    let title = self.checkParams();
    if (title != "") {
      wx.showToast({
        title: title,
        icon: "error"
      });
      return;
    }

    let params = {
      workerUpcountApprovalId: self.data.taskInfo.workerUpcountApprovalId,
      approvalResult: self.data.approvalResult,
      rejectReason: self.data.rejectReason,
      throughQuantity: self.data.throughQuantity,
    };

    app.fetchApis(self, qCloud.api.workreport_submitWorkReportApproval, params, "POST", function (e) {
      let result = e.data;
      if (result.code == 200) {
        wx.showToast({
          title: result.msg,
          icon: "success"
        });

        // 查询上数审批列表
        self.queryUpcountApprovalList();
      }
    });
    this.setData({
      taskInfo: {},
      approvalResult: 0,
      throughQuantity: null,
      rejectReason: null,
    });
  },
  // 校验参数
  checkParams() {
    let self = this;

    if (self.data.approvalResult != 1 && self.data.approvalResult != 2) {
      return "审批异常";
    }

    // 通过数量:1-审批通过时必送
    if (self.data.approvalResult == 1) {
      if (!self.data.throughQuantity) {
        return "请输入通过数量";
      }
      if (self.data.throughQuantity.trim() == "") {
        this.setData({
          throughQuantity: null
        });
        return "请输入通过数量";
      }
      this.setData({
        throughQuantity: self.data.throughQuantity.trim()
      });
      if (!(/(^[0-9]*$)/.test(self.data.throughQuantity))) {
        return "请输入正整数";
      }
      if (Number(self.data.throughQuantity) <= 0) {
        return "通过数量大于0";
      }
      this.setData({
        throughQuantity: Number(self.data.throughQuantity)
      });
    }

    // 拒绝原因:2-审批拒绝时必送
    if (self.data.approvalResult == 2) {
      if (!self.data.rejectReason) {
        return "请输入拒绝原因";
      }
      if (self.data.rejectReason.trim() == "") {
        this.setData({
          rejectReason: null
        });
        return "请输入拒绝原因";
      }
      this.setData({
        rejectReason: self.data.rejectReason.trim()
      });
    }
    return "";
  },
  /** 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {},
  /** 生命周期函数--监听页面显示 */
  onShow: function () {},
  /** 生命周期函数--监听页面隐藏 */
  onHide: function () {},
  /** 生命周期函数--监听页面卸载 */
  onUnload: function () {},
  /** 页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {
    // 查询上数审批列表
    this.queryUpcountApprovalList();
  },
  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function () {},
  /** 用户点击右上角分享 */
  onShareAppMessage: function () {}
});
